<?php
/**
 * FastApp.
 * 10/22/24
 * @Author deepsea159
 * @Contact telegram:deepsea159
 */

namespace App\Game\Api\Service;

use App\Fastapp\Api\Service\BaseService;
use App\Game\Model\GameAccount;
use App\Marketing\Model\MarketingGradient;
use App\Marketing\Model\MarketingGradientLog;

class GradientService extends BaseService
{
    public function invite(): array
    {
        $account_type = fastapp_header('account-type');
        $md = MarketingGradient::query()->where(['account_type' => $account_type, 'code' => 'invite'])->first();
        if (!$md) return [];
        try {
            $uid = get_uid();
        } catch (\Throwable) {
            $uid = 0;
        }
        $logs = MarketingGradientLog::query()->where(['gradient_id' => $md->id, 'user_id' => $uid])->get();
        $s = $logs->pluck('get_at', 'idx')->toArray();
        $log_id = $logs->pluck('id', 'idx')->toArray();
        $list = [];
        foreach ($md->config as $k => $item) {
            $item['status'] = isset($s[$k]) ? ($s[$k] ? 2 : 1) : 0;
            $item['log_id'] = $log_id[$k] ?? 0;
            $list[] = $item;
        }
        $setting = api_cache()->getAppSetting($account_type);
        $num = GameAccount::query()->where(['parent_id' => $uid, 'status' => 1])->count();
        return array_merge(['list' => $list, 'num' => $num], [
            'open_box_recharge' => $setting['open_box_recharge'],
            'open_box_bet' => $setting['open_box_bet']
        ]);
    }

    public function activeChildren(): array
    {
        $uid = get_uid();
        $paginate = GameAccount::query()->with(['account:gene_id,username,mobile,code,created_at'])
            ->where(['parent_id' => $uid, 'status' => 1])->orderByDesc('id')
            ->simplePaginate();
        $list = [];
        foreach ($paginate->items() as $item) $list[] = $item->account;
        return ['list' => $list];
    }
}